
//
//  HMSportTracking.m
//  HMBest22
//
//  Created by Apple on 17/1/9.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "HMSportTracking.h"
NSString *const HMSportTrackingNotification = @"HMSportTrackingNotification";
@interface HMSportTracking ()
// 起始位置
@property (nonatomic, strong) CLLocation *startLocation;
/// 所有的两点之间模型的  数组
@property (nonatomic, strong) NSMutableArray *trackingLines;

/// 用户的上一个位置
@property (nonatomic, strong) CLLocation *preGPSLocation;
@end
@implementation HMSportTracking
- (instancetype)initWithSportType:(HMSportType)sportType andSportState:(HMSportState)sportState
{
    self = [super init];
    if (self) {
        _sportType = sportType;
        _sportState = sportState;
        _trackingLines = [NSMutableArray array];
    }
    return self;
}

- (UIImage *)image
{
    // 获取图片
    UIImage *image;
    switch (self.sportType) {
        case HMSportTypeRun:
            image = [UIImage imageNamed:@"map_annotation_run"];
            break;
        case HMSportTypeWalk:
            image = [UIImage imageNamed:@"map_annotation_walk"];
            break;
        case HMSportTypeBike:
            image = [UIImage imageNamed:@"map_annotation_bike"];
            break;
    }
    return image;
}

- (void)setSportState:(HMSportState)sportState
{
    _sportState = sportState;
    // 判断当前的运动状态   如果不是继续   直接将开始的位置置为nil
    if (self.sportState != HMSportStateContinue) {
        self.startLocation = nil;
    }
}

- (CLLocation *)userLocation
{
    // 获取用户运动的第一个位置的模型
    HMSportTrackingLine *trackingLine = self.trackingLines.firstObject;
    return trackingLine.startLocation;
}
/// 比较两个位置之间的时间差 -->GPS信号
- (HMSportGPSSingalState)gpsSingalWithLocation:(CLLocation *)location
{
    HMSportGPSSingalState state = HMSportGPSSingalStateBad;
    // 当用户速度为-1时,大部分情况是在室内
    // 从室外到室内过程中   信号会非常差   反应迟钝
    if (location.speed < 0) {
//        NSLog(@"用户可能在室内  %@",location);
        [self postNotification:state];
        return state;
    }
    
    if (_preGPSLocation == nil) {
//        NSLog(@"第一个点");
        [self postNotification:state];
        _preGPSLocation = location;
        return state;
    }
    // 差值在 1.0左右表示信号非常好 !
//    NSLog(@"%f",[location.timestamp timeIntervalSinceDate:_preGPSLocation.timestamp]);
    NSTimeInterval delta = ABS([location.timestamp timeIntervalSinceDate:_preGPSLocation.timestamp]);
    
    delta = ABS(delta - 1);
    if (delta < 0.01) {// 信号非常好
        state = HMSportGPSSingalStateGood;
        [self postNotification:state];
    }else if(delta < 1){// 普通
        
        state = HMSportGPSSingalStateNormal;
        [self postNotification:state];
        
    }

    _preGPSLocation = location;
    return state;
}

- (void)postNotification:(HMSportGPSSingalState)state
{
    //  发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:HMSportTrackingNotification object:@(state)];
}
// 修改用户真正的运动状态
- (void)checkUserLocation:(CLLocation *)location
{
    if (location.speed == 0 && self.sportState == HMSportStateContinue) {
        self.sportState = HMSportStatePause;
//        NSLog(@"用户运动状态变成暂停");
    }else if (location.speed >0 && self.sportState == HMSportStatePause)
    {
        self.sportState = HMSportStateContinue;
//        NSLog(@"用户运动状态变成继续");
    }
        
    
}
- (HMSportPolyline *)appendLocation:(CLLocation *)location
{
    // 如果用户没有移动,就不需要画线  速度为0代表用户没有移动
//    NSLog(@"%f",location.speed);
    if ([self gpsSingalWithLocation:location] < HMSportGPSSingalStateNormal) {
        return nil;
    }
    
    // 检测用户位置是否改变  -->用户的运动状态是否改变
    [self checkUserLocation:location];
    // 如果运动状态不是继续   就返回
    if (self.sportState != HMSportStateContinue) {
        return nil;
    }
    
    if (self.startLocation == nil) {
        self.startLocation = location;
        return nil;
    }
//    NSLog(@"总时间  %f  总距离 %f   平均速度 %f  最大速度  %f",self.totalTime,self.totalDistance,self.avgSpeed,self.maxSpeed);
    
    // 获取折线
    HMSportTrackingLine *trackingLine = [[HMSportTrackingLine alloc] initWithStartLocation:self.startLocation andEndLocation:location];
    // 添加模型到数组中
    [self.trackingLines addObject:trackingLine];
    
    self.startLocation = location;
    return trackingLine.polyline;
}

- (double)totalTime
{
    // 通过KVC使用
    return [[self.trackingLines valueForKeyPath:@"@sum.time"] doubleValue];
}

- (double)totalDistance
{
    return [[self.trackingLines valueForKeyPath:@"@sum.distance"] doubleValue];
}

- (double)avgSpeed
{
    return [[self.trackingLines valueForKeyPath:@"@avg.speed"] doubleValue];
}

- (double)maxSpeed
{
    return [[self.trackingLines valueForKeyPath:@"@max.speed"] doubleValue];
}

- (NSString *)totalTimeStr
{
    //  时间  00:00:00
    NSInteger time = (NSInteger)self.totalTime;
    // 小时
    NSInteger hour = time / 3600;
    // 分钟
    NSInteger minute = (time % 3600) / 60;
    // 秒
    NSInteger second = time % 60;
    
    return  [NSString stringWithFormat:@"%02zd:%02zd:%02zd",hour,minute,second];
}
@end
